package com.example.jegotrip.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;
import com.example.jegotrip.common.CornerImageView;
import com.example.jegotrip.entity.ProductInfo;

import java.util.ArrayList;
import java.util.List;

public class RightShopListAdapter extends RecyclerView.Adapter<RightShopListAdapter.RightShopViewHolder> {

    private List<ProductInfo> mProductInfos = new ArrayList<>();
    private int currentIndex = 0;

    public void setListData(List<ProductInfo> list){
        this.mProductInfos = list;
        notifyDataSetChanged();
    }

    public RightShopListAdapter(List<ProductInfo> dataList) {
        this.mProductInfos = dataList;
    }

    @NonNull
    @Override
    public RightShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_shop_list_item,null);
        return new RightShopViewHolder(view);
    }

    @Override
    @SuppressLint({"RecyclerView", "SetTextI18n"})
    public void onBindViewHolder(@NonNull RightShopViewHolder holder, int position) {

        ProductInfo product = mProductInfos.get(position);
        holder.product_img.setImageURL(product.getProduct_img());
        holder.product_title.setText(product.getProduct_title());
        holder.product_details.setText(product.getProduct_details());
//        这里一定要注意，一定要设置文本类型
        holder.product_price.setText(product.getProduct_price() + "");
//        一级分类点击
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mRightListOnClickItemListener != null){
                    mRightListOnClickItemListener.onItemClick(product,position);
                }
            }
        });

        if (currentIndex == position){
            holder.itemView.setBackgroundResource(R.drawable.type_selector_bg);
        } else {
            holder.itemView.setBackgroundResource(R.drawable.type_selector_normal);

        }
    }

    @Override
    public int getItemCount() {
        return mProductInfos.size();
    }

    static class RightShopViewHolder extends RecyclerView.ViewHolder{

        private CornerImageView product_img;
        private TextView product_title;
        private TextView product_details;
        private TextView product_price;
        public RightShopViewHolder(@NonNull View itemView) {
            super(itemView);
            product_img = itemView.findViewById(R.id.product_img);
            product_title = itemView.findViewById(R.id.product_title);
            product_details = itemView.findViewById(R.id.product_details);
            product_price = itemView.findViewById(R.id.product_price);
        }
    }

    public void setmRightListOnClickItemListener(RightListOnClickItemListener mRightListOnClickItemListener) {
        this.mRightListOnClickItemListener = mRightListOnClickItemListener;
    }

    private RightListOnClickItemListener mRightListOnClickItemListener;

    public interface RightListOnClickItemListener {
        void onItemClick(ProductInfo productInfo, int position);
    }

    public void setCurrentIndex(int position){
        this.currentIndex = position;
//        这句话不能少！！！
        notifyDataSetChanged();

    }
}
